package com.springclouddemo.gatewayzuulaggregation.services;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import rx.Observable;

/**
 * @author 何昌杰
 */
@Service
public class AggregationService {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public Observable<String> helloDemo1(String name) {
        // 创建一个被观察者
        return Observable.create(observer -> {
            // 请求微服务1的/hello/{name}端点
            String res = restTemplate.getForObject("http://eureka-client-consumer-feign/hello/{name}", String.class, name);
            observer.onNext(res);
            observer.onCompleted();
        });
    }

    @HystrixCommand(fallbackMethod = "fallback")
    public Observable<String> helloDemo2(String name) {
        return Observable.create(syncOnSubscribe -> {
            // 请求微服务2的/hello/{name}端点
            String res = restTemplate.getForObject("http://eureka-client-consumer-hystrix/hello/{name}", String.class, name);
            syncOnSubscribe.onNext(res);
            syncOnSubscribe.onCompleted();
        });
    }

    public String fallback(String name) {
        return "默认值："+name;
    }

}
